package com.example.assign2_list;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class AddCountry extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_country);
    }
    
    public void addToList(View v){
    	EditText year = (EditText) findViewById(R.id.year);
    	EditText country = (EditText) findViewById(R.id.country);
    	
    	if(year.getText().length() == 0 || country.getText().length() == 0)
    	{
    		Toast.makeText(this, R.string.addError, Toast.LENGTH_SHORT).show();
    	}
    	else{
    		int yearInt = Integer.parseInt(year.getText().toString());
    		String countryString = country.getText().toString();
    		/*	Add information to db	*/
    		MyCountries.dbDataSource.createTask(yearInt, countryString);
    		addNewToList();
    		finish();		//	Close window when done
    	}
    }
    
    private void addNewToList(){
    	int taskNr = MyCountries.countryList.size()+1;
    	MyCountries.countryList.add(MyCountries.dbDataSource.getTask(taskNr));
    }
}
